//
//  LZBillboardViewController.m
//  沙哈拉FM
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 S1mpl3. All rights reserved.
//

#import "LZBillboardViewController.h"
#import "TabBarController.h"
#import "LZTableViewCell.h"
#import "FitHeader.h"
#import "TwoToOne.h"
#import "LZBillboardModel.h"
#import "LZFavouriteTableViewCell.h"
#import "LZPlayViewController.h"
@interface LZBillboardViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)TabBarController *tabBar;
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *dataSourceArray;
@property(nonatomic, strong)NSArray *array;
@end

@implementation LZBillboardViewController

#pragma mark - 视图将要出现时
- (void)viewWillAppear:(BOOL)animated{
    [[TabBarController shareInstance] remainButtonOfPlay:YES];
    [TabBarController shareInstance].tabBar.translucent = YES;
}
- (void)viewDidDisappear:(BOOL)animated{
    [[TabBarController shareInstance] remainButtonOfPlay:NO];
    [TabBarController shareInstance].tabBar.translucent = NO;
}
- (void)loadView{
    [super loadView];
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 10 * FITHeight) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
    self.title = _bill.title;
    _dataSourceArray = [NSMutableArray array];
    [self getData];
}
- (void)getData{
    NSString *urlString = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/%@?device=android&key=%@&pageId=1&pageSize=40", _bill.contentType, _bill.key];
    [TwoToOne getDataByString:urlString BodyString:nil WithDataBlock:^(id data) {
        NSArray *listArray = [data objectForKey:@"list"];
        _array = [data objectForKey:@"list"];
        
        for (NSDictionary *dic in listArray) {
            LZBillboardModel *lzModel = [[LZBillboardModel alloc]init];
            [lzModel setValuesForKeysWithDictionary:dic];
            [_dataSourceArray addObject:lzModel];
        }
        [_tableView reloadData];
        [self saveArray];
    }];
}
#pragma mark - 沙盒
- (void)saveArray{
    NSString *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString *documentPath = [documents stringByAppendingPathComponent:@"array.xml"];
    [_array writeToFile:documentPath atomically:YES];
}
#pragma mark - cell 的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSourceArray.count;
}
#pragma mark - cell的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_section == 0) {
        if (_index == 0) {
            static NSString *identifier = @"cell";
            LZTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
            if (!cell) {
                cell = [[LZTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            }
            cell.selectionStyle = 0;
            LZBillboardModel *lzModel = _dataSourceArray[indexPath.row];
            cell.lzModel = lzModel;
            cell.createdAtLabel.textColor = [UIColor colorWithRed:0.38 green:0.38 blue:0.38 alpha:1];
            cell.createdAtLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
            if (indexPath.row == 0) {
                cell.createdAtLabel.textColor = [UIColor redColor];
            }else if (indexPath.row == 1){
                cell.createdAtLabel.textColor = [UIColor colorWithRed:0.98 green:0.52 blue:0 alpha:1];
            }else if (indexPath.row == 2){
                cell.createdAtLabel.textColor = [UIColor colorWithRed:0.59 green:0.69 blue:0 alpha:1];
            }
            return cell;
        }else{
            static NSString *identifier1 = @"cell1";
            LZFavouriteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier1];
            if (!cell) {
                cell = [[LZFavouriteTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier1];
            }
            cell.selectionStyle = 0;
            [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
            LZBillboardModel *lzModel = _dataSourceArray[indexPath.row];
            cell.lzModel = lzModel;
            cell.createdAtLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
            cell.createdAtLabel.textColor = [UIColor colorWithRed:0.38 green:0.38 blue:0.38 alpha:1];
            if (indexPath.row == 0) {
                cell.createdAtLabel.textColor = [UIColor redColor];
            }else if (indexPath.row == 1){
                cell.createdAtLabel.textColor = [UIColor colorWithRed:0.98 green:0.52 blue:0 alpha:1];
            }else if (indexPath.row == 2){
                cell.createdAtLabel.textColor = [UIColor colorWithRed:0.59 green:0.69 blue:0 alpha:1];
            }
            return cell;
        }
    }else{
        static NSString *identifier = @"cell";
        LZFavouriteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[LZFavouriteTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.selectionStyle = 0;
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        LZBillboardModel *lzModel = _dataSourceArray[indexPath.row];
        cell.lzModel1 = lzModel;
        cell.createdAtLabel.textColor = [UIColor colorWithRed:0.38 green:0.38 blue:0.38 alpha:1];
        cell.createdAtLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
        if (indexPath.row == 0) {
            cell.createdAtLabel.textColor = [UIColor redColor];
        }else if (indexPath.row == 1){
            cell.createdAtLabel.textColor = [UIColor colorWithRed:0.98 green:0.52 blue:0 alpha:1];
        }else if (indexPath.row == 2){
            cell.createdAtLabel.textColor = [UIColor colorWithRed:0.59 green:0.69 blue:0 alpha:1];
        }
        return cell;
    }
}
#pragma mark - 跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LZPlayViewController *lzPlay = [[LZPlayViewController alloc]init];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:lzPlay];
    lzPlay.modalTransitionStyle = 0;
    LZBillboardModel *lzBill = _dataSourceArray[indexPath.row];
    lzPlay.lzBill = lzBill;
    [self presentViewController:nav animated:YES completion:nil];
}
#pragma mark - cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_section == 0 && _index == 0) {
        return 80 * FITHeight;
    }
    return 90 * FITHeight;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
